package at.ltasat.classifier.dt;

import at.ltasat.classifier.Membership;
import at.ltasat.classifier.ObjectFeatures;

final class DecisionNode<D> extends Node<D> {

	private final int feature;
	private final double split;
	private final Node<D> lower, upper;

	public DecisionNode(int feature, double split, Node<D> lower, Node<D> upper) {
		assert lower != null;
		assert upper != null;
		this.feature = feature;
		this.split = split;
		this.lower = lower;
		this.upper = upper;
	}

	public int getFeature() {
		return feature;
	}

	public double getSplit() {
		return split;
	}

	public Node<D> getLower() {
		return lower;
	}

	public Node<D> getUpper() {
		return upper;
	}

	@Override
	public Membership<D>[] evaluate(ObjectFeatures features) {
		assert features != null;
		final double value = features.getFeatureValue(feature);
		if (value < split) {
			return lower.evaluate(features);
		} else {
			return upper.evaluate(features);
		}
	}
	
	@Override
	public String toString(){
		return String.format("if (features[%d] < %f) then (%s) else (%s)", feature, split, lower.toString(), upper.toString());
	}

}
